<?php

namespace app\admin\controller\service\config;

use app\common\controller\Backend;
use think\Db;
/**
 * 微信推送配置
 *
 * @icon fa fa-circle-o
 */
class Template extends Backend
{

    /**
     * Template模型对象
     * @var \app\admin\model\service\config\Template
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\service\config\Template;

    }



    public function info()
    {
        $row = db('service_mini_template')->where('state',1)->find();
        if(!$row)
        {
            \app\admin\model\service\config\Template::create(['user_notice_template'=>'{"template_id":"wh-Tf1K9BCulm","name":"name5","cate":"thing14","address":"thing2","time":"date11","note":"thing12","url":"\/service\/orderdetail\/orderdetail?id="}','user_order_template'=>'{"template_id":"VdVVUljAyh1b","status":"thing4","cate":"thing10","time":"date3","note":"thing9","url":"\/service\/orderdetail\/orderdetail?id="}','order_finish_template'=>'{"template_id":"PfrBkPykVKjiTu-","status":"thing3","cate":"thing6","time":"date4","note":"thing16","url":"\/service\/orderdetail\/orderdetail?id="}','user_sales_template'=>'{"template_id":"Hu01nKDn38LpvK","cate":"thing1","status":"phrase2","time":"time3","note":"thing4","url":"\/service\/orderdetail\/orderdetail?id="}','user_complaint_template'=>'{"template_id":"k0md6xBNYt","content":"thing2","time":"time3","res":"thing4","note":"thing8","url":"\/service\/orderdetail\/orderdetail?id="}','skill_order_template'=>'{"template_id":"TvLP6DjG0fjEO","price":"amount10","cate":"thing4","time":"date5","address":"thing22","note":"thing9","url":"\/pages\/orderDetail\/orderDetail?id="}','skill_sales_template'=>'{"template_id":"G9hlX2ZNyezWIt","cate":"thing2","status":"phrase3","note":"thing4","url":"\/pages\/orderDetail\/orderDetail?id="}','shop_order_template'=>'{"template_id":"hCkCJlrJBBiYZCqM","price":"amount10","cate":"thing4","time":"thing7","address":"thing22","note":"thing9","url":"\/pages\/orderDetail\/orderDetail?order_id="}','shop_finish_template'=>'{"template_id":"B4dFH-","status":"phrase11","price":"amount6","name":"thing16","cate":"thing10","url":"\/pages\/orderDetail\/orderDetail?order_id="}','shop_sales_template'=>'{"template_id":"xRflDWHpUNha8iKq","cate":"thing2","status":"phrase3","note":"thing4","url":"\/pages\/orderDetail\/orderDetail?order_id="}','state'=>1]);
            $row = db('service_mini_template')->where('state',1)->find();
        }
        if($this->request->isPost()){
            $params = input('post.');

            $data = [
                'user_notice_template' => json_encode($params['userNotice']),
                'user_order_template' => json_encode($params['userOrder']),
                'order_finish_template' => json_encode($params['orderFinish']),
                'user_sales_template' => json_encode($params['userSales']),
                'user_complaint_template' => json_encode($params['userComplaint']),
                'skill_order_template' => json_encode($params['skillOrder']),
                'skill_sales_template' => json_encode($params['skillSales']),
                'shop_sales_template' => json_encode($params['shopSales']),
                'shop_order_template' => json_encode($params['shopOrder']),
                'shop_finish_template' => json_encode($params['shopFinish'])
            ];
            $data['updatetime'] = time();
            db('service_mini_template')->where('state',1)->update($data);
            $this->success('配置已更新');
        }
        $userNotice = $row['user_notice_template']?json_decode($row['user_notice_template'],true):'';
        $userOrder = $row['user_order_template']?json_decode($row['user_order_template'],true):'';
        $orderFinish = $row['order_finish_template']?json_decode($row['order_finish_template'],true):'';
        $userSales = $row['user_sales_template']?json_decode($row['user_sales_template'],true):'';
        $userComplaint = $row['user_complaint_template']?json_decode($row['user_complaint_template'],true):'';
        $skillOrder = $row['skill_order_template']?json_decode($row['skill_order_template'],true):'';
        $skillSales = $row['skill_sales_template']?json_decode($row['skill_sales_template'],true):'';
        $shopSales = $row['shop_sales_template']?json_decode($row['shop_sales_template'],true):'';
        $shopOrder = $row['shop_order_template']?json_decode($row['shop_order_template'],true):'';
        $shopFinish = $row['shop_finish_template']?json_decode($row['shop_finish_template'],true):'';
        $this->assign([
            'userNotice' => $userNotice,
            'userOrder' => $userOrder,
            'orderFinish' => $orderFinish,
            'userSales' =>$userSales,
            'userComplaint' => $userComplaint,
            'skillOrder' => $skillOrder,
            'skillSales' => $skillSales,
            'shopOrder' => $shopOrder,
            'shopSales' => $shopSales,
            'shopFinish' => $shopFinish
        ]);
        return $this->view->fetch();
    }


}
